/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.ingsoc2;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author sebastian
 */
class VcPrincipalModel {

    PeliculaDAO pDAO;
    GeneroDAO gDAO;

    public VcPrincipalModel(PeliculaDAO pDAO, GeneroDAO gDAO) {
        this.pDAO = pDAO;
        this.gDAO = gDAO;
    }

    public boolean validar(String usr, String pass) {
        return usr.equals("admin") && pass.equals("4dm1n");
    }

    public List<Pelicula> filtrar(int pID, String txtTitulo, String txtDirector, Genero mGenero) throws SQLException {
        if (pID >= 0) {
            return pDAO.listar(pID);
        } else {
            if (txtDirector.equals("") && txtTitulo.equals("") && !mGenero.isSet()) {
                return pDAO.listar();
            } else {
                return pDAO.listar(txtTitulo, txtDirector, mGenero);
            }
        }

    }

    public List<Pelicula> listar() throws SQLException {
        return pDAO.listar();
    }

    public Collection<Genero> listarGenero() throws SQLException {
        return gDAO.listar();

    }

    public void eliminarPelicula(ArrayList<Integer> peliIdList) throws SQLException {

        for (Integer peliId : peliIdList) {

            Pelicula pNueva = new Pelicula();
            pNueva.setId(peliId);
            pDAO.borrar(pNueva);

        }

    }

    public Boolean guardarPelicula(Pelicula pNueva) throws SQLException {
        //TODO Aca tiene que conocer el DAO y guardar, debe recibir la entidad a guardar, no la vista
        //TODO Consolidar las acciones nuevo, guardar y eliminar en el mismmo modelo.
        if (pNueva.getId() >= 0) {
            return pDAO.guardar(pNueva);

        } else {
            return pDAO.crear(pNueva);

        }
    }

    public Pelicula mostrarPelicula(int pID) throws SQLException {
        return pDAO.listar(pID).get(0);

    }

}
